
package data;

import java.sql.*;
import java.util.*;
import utils.Transacao;

public class UserData {

    public void incluir(UserDO user, Transacao tr) throws Exception {
     Connection con = tr.obterConexao();
     String sql = "insert into mydb.User (Login, Password, Paciente_idPaciente, Funcionario_idFuncionario) values (?, ?, ?, ?)";
     PreparedStatement ps = con.prepareStatement(sql);
     ps.setString(1, user.getLogin());
     ps.setString(2, user.getPassword());
     ps.setInt(3, user.getIdPaciente());
     ps.setInt(4, user.getIdFuncionario());
     int result = ps.executeUpdate();
    }

    public void excluir(UserDO user, Transacao tr) throws Exception {
        excluir(user.getIdFuncionario(), tr);
    }

    public void excluir(int idobj, Transacao tr) throws Exception {
        Connection con = tr.obterConexao();
        String sql = "delete from mydb.User where idUser=?";
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setInt(1, idobj);
        int result = ps.executeUpdate();
    }

    public void atualizar(UserDO user, Transacao tr) throws Exception {
        Connection con = tr.obterConexao();
        String sql = "update Funcionario set Login=?, Password=?, Paciente_idPaciente=?, Funcionario_idFuncionario=?, where idUser=?";
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setString(1, user.getLogin());
        ps.setString(2, user.getPassword());
        ps.setInt(3, user.getIdPaciente());
        ps.setInt(4, user.getIdFuncionario());
        ps.setInt(5,user.getIdUser());
        int result = ps.executeUpdate();
    }

    public UserDO buscar(int idobj, Transacao tr) throws Exception {
        Connection con = tr.obterConexao();
        String sql = "select * from mydb.User where  idUser=?";
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setInt(1, idobj);
        ResultSet rs = ps.executeQuery();
        rs.next();
        UserDO user = new UserDO();
        user.setLogin (rs.getString("Login"));
        user.setPassword (rs.getString("Password"));
        user.setIdPaciente (rs.getInt("Paciente_idPaciente"));
        user.setIdFuncionario(rs.getInt("Funcionario_idFuncionario"));
        return user;
    }
    

    public UserDO pesquisarPorNome(String login, Transacao tr) throws Exception {
        Connection con = tr.obterConexao();
        String sql = "select * from mydb.user where Login=?";
        PreparedStatement ps;
        ps = con.prepareStatement(sql);
        ps.setString(1, login);
        ResultSet rs = ps.executeQuery();
        rs.next();
        System.out.println("query executada");
        UserDO c = new UserDO();
        c.setLogin (rs.getString("Login"));
        c.setPassword (rs.getString("Password"));
        c.setIdPaciente (rs.getInt("Paciente_idPaciente"));
        c.setIdFuncionario(rs.getInt("Funcionario_idFuncionario"));
 
        return c;
    }   
    
    
}
